home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / etc / cron.daily / apt < prev    next >
Encoding:
Text File  |  2011-04-15  |  14.5 KB  |  500 lines

  1. #!/bin/sh
  2. #set -e
  3. #
  4. # This file understands the following apt configuration variables:
  5. # Values here are the default.
  6. # Create /etc/apt/apt.conf.d/02periodic file to set your preference.
  7. #
  8. #  Dir "/";
  9. #  - RootDir for all configuration files
  10. #
  11. #  Dir::Cache "var/apt/cache/";
  12. #  - Set apt package cache directory
  13. #
  14. #  Dir::Cache::Archives "archives/";
  15. #  - Set package archive directory
  16. #
  17. #  APT::Periodic::Enable "1";
  18. #  - Enable the update/upgrade script (0=disable)
  19. #
  20. #  APT::Periodic::BackupArchiveInterval "0";
  21. #  - Backup after n-days if archive contents changed.(0=disable)
  22. #
  23. #  APT::Periodic::BackupLevel "3";
  24. #  - Backup level.(0=disable), 1 is invalid.
  25. #
  26. #  Dir::Cache::Backup "backup/";
  27. #  - Set periodic package backup directory
  28. #
  29. #  APT::Archives::MaxAge "0"; (old, deprecated)
  30. #  APT::Periodic::MaxAge "0"; (new)
  31. #  - Set maximum allowed age of a cache package file. If a cache 
  32. #    package file is older it is deleted (0=disable)
  33. #
  34. #  APT::Archives::MinAge "2"; (old, deprecated)
  35. #  APT::Periodic::MinAge "2"; (new)
  36. #  - Set minimum age of a package file. If a file is younger it
  37. #    will not be deleted (0=disable). Usefull to prevent races 
  38. #    and to keep backups of the packages for emergency.
  39. #
  40. #  APT::Archives::MaxSize "0"; (old, deprecated)
  41. #  APT::Periodic::MaxSize "0"; (new)
  42. #  - Set maximum size of the cache in MB (0=disable). If the cache
  43. #    is bigger, cached package files are deleted until the size
  44. #    requirement is met (the biggest packages will be deleted 
  45. #    first).
  46. #
  47. #  APT::Periodic::Update-Package-Lists "0";
  48. #  - Do "apt-get update" automatically every n-days (0=disable)
  49. #    
  50. #  APT::Periodic::Download-Upgradeable-Packages "0";
  51. #  - Do "apt-get upgrade --download-only" every n-days (0=disable)
  52. #
  53. #  APT::Periodic::Download-Upgradeable-Packages-Debdelta "1";
  54. #  - Use debdelta-upgrade to download updates if available (0=disable)
  55. #
  56. #  APT::Periodic::Unattended-Upgrade "0";
  57. #  - Run the "unattended-upgrade" security upgrade script 
  58. #    every n-days (0=disabled)
  59. #    Requires the package "unattended-upgrades" and will write
  60. #    a log in /var/log/unattended-upgrades
  61. #  APT::Periodic::AutocleanInterval "0";
  62. #  - Do "apt-get autoclean" every n-days (0=disable)
  63. #
  64. #  APT::Periodic::Verbose "0";
  65. #  - Send report mail to root
  66. #      0:  no report             (or null string)
  67. #      1:  progress report       (actually any string)
  68. #      2:  + command outputs     (remove -qq, remove 2>/dev/null, add -d)
  69. #      3:  + trace on            
  70.  
  71. check_stamp()
  72. {
  73.     stamp="$1"
  74.     interval="$2"
  75.  
  76.     if [ $interval -eq 0 ]; then
  77.     debug_echo "check_stamp: interval=0"
  78.     # treat as no time has passed
  79.         return 1
  80.     fi
  81.  
  82.     if [ ! -f $stamp ]; then
  83.     debug_echo "check_stamp: missing time stamp file: $stamp."
  84.     # treat as enough time has passed
  85.         return 0
  86.     fi
  87.  
  88.     # compare midnight today to midnight the day the stamp was updated
  89.     stamp_file="$stamp"
  90.     stamp=$(date --date=$(date -r $stamp_file --iso-8601) +%s 2>/dev/null)
  91.     if [ "$?" != "0" ]; then
  92.         # Due to some timezones returning 'invalid date' for midnight on
  93.         # certain dates (eg America/Sao_Paulo), if date returns with error
  94.         # remove the stamp file and return 0. See coreutils bug:
  95.         # http://lists.gnu.org/archive/html/bug-coreutils/2007-09/msg00176.html
  96.         rm -f "$stamp_file"
  97.         return 0
  98.     fi
  99.  
  100.     now=$(date --date=$(date --iso-8601) +%s 2>/dev/null)
  101.     if [ "$?" != "0" ]; then
  102.         # As above, due to some timezones returning 'invalid date' for midnight
  103.         # on certain dates (eg America/Sao_Paulo), if date returns with error
  104.         # return 0.
  105.         return 0
  106.     fi
  107.  
  108.     delta=$(($now-$stamp))
  109.  
  110.     # intervall is in days, convert to sec.
  111.     interval=$(($interval*60*60*24))
  112.     debug_echo "check_stamp: interval=$interval, now=$now, stamp=$stamp, delta=$delta (sec)"
  113.  
  114.     # remove timestamps a day (or more) in the future and force re-check
  115.     if [ $stamp -gt $(($now+86400)) ]; then
  116.          echo "WARNING: file $stamp_file has a timestamp in the future: $stamp"
  117.          rm -f "$stamp_file"
  118.          return 0
  119.     fi
  120.  
  121.     if [ $delta -ge $interval ]; then
  122.         return 0
  123.     fi
  124.  
  125.     return 1
  126. }
  127.  
  128. update_stamp()
  129. {
  130.     stamp="$1"
  131.     touch $stamp
  132. }
  133.  
  134. # we check here if autoclean was enough sizewise
  135. check_size_constraints()
  136. {
  137.     MaxAge=0
  138.     eval $(apt-config shell MaxAge APT::Archives::MaxAge)
  139.     eval $(apt-config shell MaxAge APT::Periodic::MaxAge)
  140.  
  141.     MinAge=2
  142.     eval $(apt-config shell MinAge APT::Archives::MinAge)
  143.     eval $(apt-config shell MinAge APT::Periodic::MinAge)
  144.  
  145.     MaxSize=0
  146.     eval $(apt-config shell MaxSize APT::Archives::MaxSize)
  147.     eval $(apt-config shell MaxSize APT::Periodic::MaxSize)
  148.  
  149.     Cache="/var/cache/apt/archives/"
  150.     eval $(apt-config shell Cache Dir::Cache::archives/d)
  151.  
  152.     # sanity check
  153.     if [ -z "$Cache" ]; then
  154.     echo "empty Dir::Cache::archives, exiting"
  155.     exit
  156.     fi
  157.  
  158.     # check age
  159.     if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then
  160.     debug_echo "aged: ctime <$MaxAge and mtime <$MaxAge and ctime>$MinAge and mtime>$MinAge"
  161.     find $Cache -name "*.deb"  \( -mtime +$MaxAge -and -ctime +$MaxAge \) -and -not \( -mtime -$MinAge -or -ctime -$MinAge \) -print0 | xargs -r -0 rm -f
  162.     elif [ ! $MaxAge -eq 0 ]; then
  163.     debug_echo "aged: ctime <$MaxAge and mtime <$MaxAge only"
  164.     find $Cache -name "*.deb"  -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f
  165.     else
  166.     debug_echo "skip aging since MaxAge is 0"
  167.     fi
  168.     
  169.     # check size
  170.     if [ ! $MaxSize -eq 0 ]; then
  171.     # maxSize is in MB
  172.     MaxSize=$(($MaxSize*1024))
  173.  
  174.     #get current time
  175.     now=$(date --date=$(date --iso-8601) +%s)
  176.     MinAge=$(($MinAge*24*60*60))
  177.  
  178.     # reverse-sort by mtime
  179.     for file in $(ls -rt $Cache/*.deb 2>/dev/null); do 
  180.         du=$(du -s $Cache)
  181.         size=${du%%/*}
  182.         # check if the cache is small enough
  183.         if [ $size -lt $MaxSize ]; then
  184.         debug_echo "end remove by archive size:  size=$size < $MaxSize"
  185.         break
  186.         fi
  187.  
  188.         # check for MinAge of the file
  189.         if [ $MinAge -ne 0 ]; then 
  190.         # check both ctime and mtime 
  191.         mtime=$(stat -c %Y $file)
  192.         ctime=$(stat -c %Z $file)
  193.         if [ $mtime -gt $ctime ]; then
  194.             delta=$(($now-$mtime))
  195.         else
  196.             delta=$(($now-$ctime))
  197.         fi
  198.         if [ $delta -le $MinAge ]; then
  199.             debug_echo "skip remove by archive size:  $file, delta=$delta < $MinAgeSec"
  200.             break
  201.         else
  202.             # delete oldest file
  203.             debug_echo "remove by archive size: $file, delta=$delta >= $MinAgeSec (sec), size=$size >= $MaxSize"
  204.             rm -f $file
  205.         fi
  206.         fi
  207.     done
  208.     fi
  209. }
  210.  
  211. # deal with the Apt::Periodic::BackupArchiveInterval
  212. do_cache_backup()
  213. {
  214.     BackupArchiveInterval="$1"
  215.     if [ $BackupArchiveInterval -eq 0 ]; then
  216.     return
  217.     fi
  218.  
  219.     # Set default values and normalize
  220.     CacheDir="/var/cache/apt"
  221.     eval $(apt-config shell CacheDir Dir::Cache/d)
  222.     CacheDir=${CacheDir%/}
  223.     if [ -z "$CacheDir" ]; then
  224.     debug_echo "practically empty Dir::Cache, exiting"
  225.     return 0
  226.     fi
  227.  
  228.     Cache="${CacheDir}/archives/"
  229.     eval $(apt-config shell Cache Dir::Cache::Archives/d)
  230.     if [ -z "$Cache" ]; then
  231.     debug_echo "practically empty Dir::Cache::archives, exiting"
  232.     return 0
  233.     fi
  234.  
  235.     BackupLevel=3
  236.     eval $(apt-config shell BackupLevel APT::Periodic::BackupLevel)
  237.     if [ $BackupLevel -le 1 ]; then 
  238.     BackupLevel=2 ; 
  239.     fi
  240.     
  241.     Back="${CacheDir}/backup/"
  242.     eval $(apt-config shell Back Dir::Cache::Backup/d)
  243.     if [ -z "$Back" ]; then
  244.     echo "practically empty Dir::Cache::Backup, exiting" 1>&2
  245.     return
  246.     fi
  247.  
  248.     CacheArchive="$(basename "${Cache}")"
  249.     test -n "${CacheArchive}" || CacheArchive="archives"
  250.     BackX="${Back}${CacheArchive}/"
  251.     for x in $(seq 0 1 $((${BackupLevel}-1))); do 
  252.     eval "Back${x}=${Back}${x}/"
  253.     done
  254.     
  255.     # backup after n-days if archive contents changed.
  256.     # (This uses hardlink to save disk space)
  257.     BACKUP_ARCHIVE_STAMP=/var/lib/apt/periodic/backup-archive-stamp
  258.     if check_stamp $BACKUP_ARCHIVE_STAMP $BackupArchiveInterval; then
  259.     if [ $({(cd $Cache 2>/dev/null; find . -name "*.deb"); (cd $Back0 2>/dev/null;find . -name "*.deb") ;}| sort|uniq -u|wc -l) -ne 0 ]; then
  260.         mkdir -p $Back
  261.         rm -rf $Back$((${BackupLevel}-1))
  262.         for y in $(seq $((${BackupLevel}-1)) -1 1); do 
  263.         eval BackY=${Back}$y
  264.         eval BackZ=${Back}$(($y-1))
  265.         if [ -e $BackZ ]; then 
  266.             mv -f $BackZ $BackY ; 
  267.         fi
  268.         done
  269.         cp -la $Cache $Back ; mv -f $BackX $Back0
  270.         update_stamp $BACKUP_ARCHIVE_STAMP
  271.         debug_echo "backup with hardlinks. (success)"
  272.     else
  273.         debug_echo "skip backup since same content."
  274.     fi
  275.     else
  276.     debug_echo "skip backup since too new."
  277.     fi
  278. }
  279.  
  280. # sleep for a random interval of time (default 30min)
  281. # (some code taken from cron-apt, thanks)
  282. random_sleep()
  283. {
  284.     RandomSleep=1800
  285.     eval $(apt-config shell RandomSleep APT::Periodic::RandomSleep)
  286.     if [ $RandomSleep -eq 0 ]; then
  287.     return
  288.     fi
  289.     if [ -z "$RANDOM" ] ; then
  290.         # A fix for shells that do not have this bash feature.
  291.     RANDOM=$(dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -c"1-5")
  292.     fi
  293.     TIME=$(($RANDOM % $RandomSleep))
  294.     debug_echo "sleeping for $TIME seconds"
  295.     sleep $TIME
  296. }
  297.  
  298.  
  299. debug_echo()
  300. {
  301.     # Display message if $VERBOSE >= 1
  302.     if [ "$VERBOSE" -ge 1 ]; then
  303.     echo $1 1>&2
  304.     fi
  305. }
  306.  
  307. # ------------------------ main ----------------------------
  308.  
  309. # Backup the 7 last versions of APT's extended_states file
  310. # shameless copy from dpkg cron
  311. if cd /var/backups ; then
  312.     if ! cmp -s apt.extended_states.0 /var/lib/apt/extended_states; then
  313.     cp -p /var/lib/apt/extended_states apt.extended_states
  314.     savelog -c 7 apt.extended_states >/dev/null
  315.     fi
  316. fi
  317.  
  318. # check apt-config exstance
  319. if ! which apt-config >/dev/null ; then
  320.     exit 0
  321. fi
  322.  
  323. # check if the user really wants to do something
  324. AutoAptEnable=1  # default is yes
  325. eval $(apt-config shell AutoAptEnable APT::Periodic::Enable)
  326.  
  327. if [ $AutoAptEnable -eq 0 ]; then
  328.     exit 0
  329. fi
  330.  
  331. # Set VERBOSE mode from  apt-config (or inherit from environment)
  332. VERBOSE=0
  333. eval $(apt-config shell VERBOSE APT::Periodic::Verbose)
  334. debug_echo "verbose level $VERBOSE"
  335. if [ "$VERBOSE" -le 2 ]; then
  336.     # quiet for 0,1,2
  337.     XSTDOUT=">/dev/null"
  338.     XSTDERR="2>/dev/null"
  339.     XAPTOPT="-qq"
  340.     XUUPOPT=""
  341. else
  342.     XSTDOUT=""
  343.     XSTDERR=""
  344.     XAPTOPT=""
  345.     XUUPOPT="-d"
  346. fi
  347. if [ "$VERBOSE" -ge 3 ]; then
  348.     # trace output
  349.     set -x
  350. fi
  351.  
  352. # laptop check, on_ac_power returns:
  353. #       0 (true)    System is on main power
  354. #       1 (false)   System is not on main power
  355. #       255 (false) Power status could not be determined
  356. # Desktop systems always return 255 it seems
  357. if which on_ac_power >/dev/null; then
  358.     on_ac_power
  359.     POWER=$?
  360.     if [ $POWER -eq 1 ]; then
  361.     debug_echo "exit: system NOT on main power"
  362.     exit 0
  363.     elif [ $POWER -ne 0 ]; then
  364.     debug_echo "power status ($POWER) undetermined, continuing"
  365.     fi
  366.     debug_echo "system is on main power."
  367. fi
  368.  
  369. # check if we can lock the cache and if the cache is clean
  370. if which apt-get >/dev/null && ! eval apt-get check -f $XAPTOPT $XSTDERR ; then
  371.     debug_echo "error encountered in cron job with \"apt-get check\"."
  372.     exit 0
  373. fi
  374.  
  375. # Global current time in seconds since 1970-01-01 00:00:00 UTC
  376. now=$(date +%s)
  377.  
  378. # Support old Archive for compatibility.
  379. # Document only Periodic for all controling parameters of this script.
  380.  
  381. UpdateInterval=0
  382. eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists)
  383.  
  384. DownloadUpgradeableInterval=0
  385. eval $(apt-config shell DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
  386.  
  387. UnattendedUpgradeInterval=0
  388. eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
  389.  
  390. AutocleanInterval=0
  391. eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
  392.  
  393. BackupArchiveInterval=0
  394. eval $(apt-config shell BackupArchiveInterval APT::Periodic::BackupArchiveInterval)
  395.  
  396. Debdelta=1
  397. eval $(apt-config shell Debdelta APT::Periodic::Download-Upgradeable-Packages-Debdelta)
  398.  
  399. # check if we actually have to do anything that requires locking the cache
  400. if [ $UpdateInterval -eq 0 ] &&
  401.    [ $DownloadUpgradeableInterval -eq 0 ] &&
  402.    [ $UnattendedUpgradeInterval -eq 0 ] &&
  403.    [ $BackupArchiveInterval -eq 0 ] &&
  404.    [ $AutocleanInterval -eq 0 ]; then
  405.  
  406.     # check cache size
  407.     check_size_constraints
  408.  
  409.     exit 0
  410. fi
  411.  
  412. # deal with BackupArchiveInterval
  413. do_cache_backup $BackupArchiveInterval
  414.  
  415. # sleep random amount of time to avoid hitting the 
  416. # mirrors at the same time
  417. random_sleep
  418.  
  419. # include default system language so that "apt-get update" will
  420. # fetch the right translated package descriptions
  421. if [ -r /etc/default/locale ]; then
  422.     . /etc/default/locale
  423.     export LANG LANGUAGE LC_MESSAGES LC_ALL
  424. fi
  425.  
  426. # update package lists
  427. UPDATED=0
  428. UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
  429. if check_stamp $UPDATE_STAMP $UpdateInterval; then
  430.     if eval apt-get $XAPTOPT -y update $XSTDERR; then
  431.     debug_echo "download updated metadata (success)."
  432.     if which dbus-send >/dev/null && pidof dbus-daemon >/dev/null; then
  433.         if dbus-send --system / app.apt.dbus.updated boolean:true ; then
  434.         debug_echo "send dbus signal (success)"
  435.         else
  436.         debug_echo "send dbus signal (error)"
  437.         fi
  438.     else
  439.         debug_echo "dbus signal not send (command not available)"
  440.     fi
  441.     update_stamp $UPDATE_STAMP
  442.     UPDATED=1
  443.     else
  444.     debug_echo "download updated metadata (error)"
  445.     fi
  446. else
  447.     debug_echo "download updated metadata (not run)."
  448. fi
  449.     
  450. # download all upgradeable packages (if it is requested)
  451. DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
  452. if [ $UPDATED -eq 1 ] && check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
  453.     if [ $Debdelta -eq 1 ]; then
  454.         debdelta-upgrade >/dev/null 2>&1 || true
  455.     fi
  456.     if  eval apt-get $XAPTOPT -y -d dist-upgrade $XSTDERR; then
  457.     update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
  458.     debug_echo "download upgradable (success)"
  459.     else
  460.     debug_echo "download upgradable (error)"
  461.     fi
  462. else
  463.     debug_echo "download upgradable (not run)"
  464. fi
  465.  
  466. # auto upgrade all upgradeable packages
  467. UPGRADE_STAMP=/var/lib/apt/periodic/upgrade-stamp
  468. if [ $UPDATED -eq 1 ] && which unattended-upgrade >/dev/null && check_stamp $UPGRADE_STAMP $UnattendedUpgradeInterval; then
  469.     if unattended-upgrade $XUUPOPT; then
  470.     update_stamp $UPGRADE_STAMP
  471.     debug_echo "unattended-upgrade (success)"
  472.     else
  473.     debug_echo "unattended-upgrade (error)"
  474.     fi
  475. else
  476.     debug_echo "unattended-upgrade (not run)"
  477. fi
  478.  
  479. # autoclean package archive
  480. AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
  481. if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
  482.     if  eval apt-get $XAPTOPT -y autoclean $XSTDERR; then
  483.     debug_echo "autoclean (success)."
  484.     update_stamp $AUTOCLEAN_STAMP
  485.     else
  486.     debug_echo "autoclean (error)"
  487.     fi
  488. else
  489.     debug_echo "autoclean (not run)"
  490. fi
  491.  
  492. # check cache size 
  493. check_size_constraints
  494.  
  495. #
  496. #     vim: set sts=4 ai :
  497. #
  498.  
  499.